<?php
namespace app\controllers;


use app\logic\MoneyLogic;
use app\logic\PlayLogic;
use app\logic\ShopLogic;
use app\logic\UserLogic;
use yii;

class UserController extends BackendController
{


    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $useLogic = new UserLogic();
        $this->data['breadcrumbs'] = '用户管理';
        if(!isset($query['phone']))
            $query['phone'] = '';
        return $this->render('index', [
            'phone'=>$query['phone'],
            'user' => $useLogic->getUser($query, $page),
            'query' => $query
        ]);
    }

    public function actionAdd()
    {
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['用户管理' => U('user/index'), ' 添加用户' => ''];
            return $this->render('add', [
                'actionUrl' => U('user/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $userLogic = new UserLogic();
            $logicRes = $userLogic->addUser($params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增用户成功');
                jump('user/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增用户失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionEdit()
    {
        $userLogic = new UserLogic();
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['用户管理' => U('user/index'), ' 编辑用户' => ''];
            $id = Yii::$app->request->get('user_id');
            $user = $userLogic->getById($id);
            return $this->render('edit', [
                'user'=>$user,
                'actionUrl' => U('user/edit')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('user_id');
            $logicRes = $userLogic->editUserById($id,$params);
            if ($logicRes['result']) {
                Yii::$app->getSession()->setFlash('success','编辑用户成功');
                jump('user/index');
            } else {
                Yii::$app->getSession()->setFlash('error',$logicRes['msg']);
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionAddCard()
    {
        $userLogic = new UserLogic();
        $playLogic = new PlayLogic();
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['用户管理' => U('user/index'), '充值体验卡' => ''];
            $id = Yii::$app->request->get('user_id');
            $user = $userLogic->getById($id);
            return $this->render('add-card', [
                'user'=>$user,
                'play'=>$playLogic->getAllCard(),
                'actionUrl' => U('user/add-card')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('user_id');
            $logicRes = $userLogic->addCard($id,$params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','充值成功');
                jump('user/index');
            } else {
                Yii::$app->getSession()->setFlash('error',$logicRes['msg']);
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionSaveStatus()
    {
        $id = Yii::$app->request->get('user_id');
        $userLogic = new UserLogic();
        $logicRes = $userLogic->saveStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改用户状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改用户状态失败');
        }
        return $this->redirect(U('user/index'));
    }

    public function actionOtherDetail(){
        $id = Yii::$app->request->get('user_id');
        $userLogic = new UserLogic();
        $manager = $userLogic->getManager($id);
        $user = $userLogic->getById($id);

        if($user['type']==0){
            $commission = [];
        }else{
            $moneyLogic = new MoneyLogic();
            /*$commissionConfig = $moneyLogic->getCommissionConfig();
            $commissionConfigTemp = [];
            foreach($commissionConfig AS $k=>$v){
                $config_ext = json_decode($v['config_ext'],true);
                $commissionConfigTemp[(string)$config_ext['role']][(string)$config_ext['type']] = $v['config_value'];
            }*/
            $total = 0;
            $experience_total = 0;
            $sale_total = 0;
            $recovery_total = 0;
            $recharge_total = 0;
            if($user['type']==1){
                $shopLogic = new ShopLogic();
                $shops = $shopLogic->getShopListByOwner($id);
                $shop_ids = array_column($shops,'shop_id');
                $cancel_list = $moneyLogic->getCancelListAndIncomeByMyself($id,$shop_ids,$user['type']);
                foreach($cancel_list AS $key=>&$value){
                    //$value['commission'] = $value['price']*$commissionConfigTemp[(string)1][(string)$value['cancel_type']]/100;
                    if($value['commission']==0){
                        array_splice($cancel_list, $key, 1);
                        continue;
                    }
                    $total += $value['commission'];
                    if($value['cancel_type']==0){
                        $experience_total += $value['commission'];
                    }elseif($value['cancel_type']==1){
                        $recharge_total += $value['commission'];
                    }elseif($value['cancel_type']==2){
                        $sale_total += $value['commission'];
                    }elseif($value['cancel_type']==3){
                        $recovery_total += $value['commission'];
                    }
                }

            }else{
                $cancel_list = $moneyLogic->getCancelListAndIncomeByMyself($id,0,$user['type']);
                foreach($cancel_list AS $key=>&$value){
                    //$value['commission'] = $value['price']*$commissionConfigTemp[(string)2][(string)$value['cancel_type']]/100;
                    if($value['commission']==0){
                        array_splice($cancel_list, $key, 1);
                        continue;
                    }
                    $total += $value['commission'];
                    if($value['cancel_type']==0){
                        $experience_total += $value['commission'];
                    }elseif($value['cancel_type']==1){
                        $recharge_total += $value['commission'];
                    }elseif($value['cancel_type']==2){
                        $sale_total += $value['commission'];
                    }elseif($value['cancel_type']==3){
                        $recovery_total += $value['commission'];
                    }
                }
            }
            $commission = $cancel_list;
        }




        $this->data['breadcrumbs'] = ['用户管理' => U('user/index'), '收入情况' => ''];
        return $this->render('other-detail', [
            'manager'=>$manager,
            'user' => $user,
            'commission'=>$commission,
            'total'=>$total
        ]);
    }


    public function actionContract(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $shopLogic = new ShopLogic();
        $contract = $shopLogic->getContract($query, $page);
        $this->data['breadcrumbs'] = '用户管理';
        return $this->render('contract', [
            'contract' => $contract,
            'query' => $query
        ]);
    }

    public function actionAddContract(){
        $userLogic = new UserLogic();
        $shopLogic = new ShopLogic();
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['合同管理' => U('user/contract'), ' 添加合同' => ''];
            $shop = $shopLogic->getAllShop();
            $user = $userLogic->getAllNotOUser();
            return $this->render('add-contract', [
                'theme' => '/assets/',
                'shop'=>$shop,
                'user'=>$user,
                'actionUrl' => U('user/add-contract')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            if($params['type']==1){
                $shop = $shopLogic->getById($params['shop_id']);
                $user = $userLogic->getById($params['user_id']);
                if($shop['owner_user_id']){
                    Yii::$app->getSession()->setFlash('error','新增合同失败，该商家已经有店主，请编辑');
                    $this->goBack(Yii::$app->request->referrer);
                }else{
                    if($user['type']==2){
                        Yii::$app->getSession()->setFlash('error','新增合同失败，该用户已经是店长身份');
                        $this->goBack(Yii::$app->request->referrer);
                    }
                    $logicRes = $shopLogic->addContract($params);
                    if ($logicRes) {
                        Yii::$app->getSession()->setFlash('success','新增合同成功');
                        jump('user/contract');
                    } else {
                        Yii::$app->getSession()->setFlash('error','新增合同失败');
                        $this->goBack(Yii::$app->request->referrer);
                    }
                }
            }else{
                $manager = $userLogic->getManager($params['user_id']);
                $user = $userLogic->getById($params['user_id']);
                if($user['type']==1){
                    Yii::$app->getSession()->setFlash('error','新增合同失败，该用户已经是店主身份');
                    $this->goBack(Yii::$app->request->referrer);
                }
                if($manager){
                    Yii::$app->getSession()->setFlash('error','新增合同失败，该用户已经是别家商店店长');
                    $this->goBack(Yii::$app->request->referrer);
                }
                $logicRes = $userLogic->addContract($params);
                if ($logicRes) {
                    Yii::$app->getSession()->setFlash('success','新增合同成功');
                    jump('user/contract');
                } else {
                    Yii::$app->getSession()->setFlash('error','新增合同失败');
                    $this->goBack(Yii::$app->request->referrer);
                }

            }

        }
    }

    public function actionEditOwnerContract(){
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('shop_id');
            $this->data['breadcrumbs'] = ['合同管理' => U('user/contract'), '编辑店主合同' => ''];
            $shopLogic = new ShopLogic();
            $userLogic = new UserLogic();
            $shop = $shopLogic->getById($id);
            $user = $userLogic->getAllOwnerUser();
            return $this->render('edit-owner-contract', [
                'user'=>$user,
                'theme' => '/assets/',
                'shop'=>$shop,
                'actionUrl' => U('user/edit-owner-contract')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('shop_id');
            $shopLogic = new ShopLogic();
            $logicRes = $shopLogic->editOwnerContract($id,$params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','编辑店主合同成功');
                jump('user/contract');
            } else {
                Yii::$app->getSession()->setFlash('error','编辑店主合同失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionEditManager(){
        if(!Yii::$app->request->post()){
            $shop_id = Yii::$app->request->get('shop_id');
            $user_id = Yii::$app->request->get('user_id');
            $this->data['breadcrumbs'] = ['合同管理' => U('user/contract'), '编辑店长合同' => ''];
            $userLogic = new UserLogic();
            $manager = $userLogic->getManager($user_id);
            return $this->render('edit-manager', [
                'manager'=>$manager,
                'theme' => '/assets/',
                'actionUrl' => U('user/edit-manager')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('manager_id');
            $userLogic = new UserLogic();
            $logicRes = $userLogic->editManager($id,$params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','编辑店长合同成功');
                jump('user/contract');
            } else {
                Yii::$app->getSession()->setFlash('error','编辑店长合同失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionDelManager(){
        $user_id = Yii::$app->request->get('user_id');
        $userLogic = new UserLogic();
        $logicRes = $userLogic->delManager($user_id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除店长成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除店长失败');
        }
        return $this->redirect(U('user/contract'));
    }



}